package com.yjd.bi.etl.user.service;

import java.util.Calendar;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.yjd.bi.etl.comm.BaseService;
import com.yjd.bi.etl.comm.Constants;
import com.yjd.bi.etl.sql.UserSqlConstants;
import com.yjd.bi.report.model.BiDatabaseInfoModel;
import com.yjd.bi.report.util.BiUtil;
import com.yjd.comm.util.DateUtil;

@Component("userStatisticService")
public class UserStatisticService extends BaseService {

	private static final int  DAYOFSECOND = 86400;

	public void statisticEveryDayData() throws Exception {

		BiDatabaseInfoModel model = this.getBiDatabaseInfoModel();
		JdbcTemplate jt = BiUtil.getJdbcTemplate(model);
		
		
		//每次统计清空前30天数据，重新跑
		jt.update(UserSqlConstants.DELETE_USER_STATISTIC);
		
		// 统计历史数据
		long maxDate = jt.queryForInt(UserSqlConstants.GET_MAX_USERSTATISTIC_DATE);
		
		if (maxDate == 0) {
			// 没有统计过，取网站上线运营的第一天 平台上线时间（2013年12月21日00：00：00）
			maxDate = Constants.INITIALIZATION_TIME;
		}else{
			maxDate = DateUtil.dateStringToTimestamp(maxDate+"",
					DateUtil.PATTERN_yyyyMMdd)+DAYOFSECOND;
		}
		
		//获取当天的0点的时间戳
		Calendar todayStart = Calendar.getInstance();  
        todayStart.set(Calendar.HOUR_OF_DAY, 0);  
        todayStart.set(Calendar.MINUTE, 0);  
        todayStart.set(Calendar.SECOND, 0);  
        todayStart.set(Calendar.MILLISECOND, 0);
        long todayTime = todayStart.getTime().getTime()/1000L; 
		
		while (maxDate  < todayTime ) {
			// 统计前一天的用户数据
			statisticLastData(maxDate, jt);
			maxDate += DAYOFSECOND;// 加一天和当前时间比较，如果小于当前时间则继续统计
			
		}
	}

	/**
	 * 统计某天前一天的用户数据
	 */
	private void statisticLastData(long maxDate, JdbcTemplate jt)
			throws Exception {

		long beginDate = maxDate;
		long endDate = beginDate + DAYOFSECOND - 1;

		// 注册用户数
		int registerCount = jt.queryForObject(UserSqlConstants.REGISTERCOUNTSQL,new Object[] { beginDate, endDate},Integer.class);

		// 登录用户数
		int loginCount = jt.queryForObject(UserSqlConstants.LOGINCOUNTSQL, new Object[] {beginDate, endDate},Integer.class);

		// 充值用户数
		int rechargeCount = jt.queryForObject(UserSqlConstants.RECHARGECOUNTSQL,new Object[] { beginDate, endDate},Integer.class);

		// 投资用户数
		int investCount = jt.queryForObject(UserSqlConstants.INVESTCOUNTSQL,new Object[] {beginDate, endDate, beginDate, endDate, beginDate, endDate},Integer.class);
		
		//首次充值用户数
		int firstRecharge = jt.queryForObject(UserSqlConstants.FIRST_RECHARGE_SQL,new Object[] { endDate, beginDate},Integer.class);
		//提现用户数
		int cashCount = jt.queryForObject(UserSqlConstants.CASH_SQL,new Object[] { beginDate , endDate},Integer.class);
		//首次投资用户数
		int firstInvest = jt.queryForObject(UserSqlConstants.FIRST_INVEST_SQL,new Object[] {  beginDate,endDate},Integer.class);
		
		//理财卡激活用户数
		int lck_active = jt.queryForObject(UserSqlConstants.LCK_ACTIVE,new Object[] {  beginDate,endDate},Integer.class);
		
		//理财卡未激活用户数
		int lck_un_active = jt.queryForObject(UserSqlConstants.LCK_UN_ACITVE,new Object[] {endDate},Integer.class);

		Long staticsDate = Long.parseLong(DateUtil.timestampToDateString(
				beginDate, DateUtil.PATTERN_yyyyMMdd));
		
		logger.info("beginDate:"+beginDate+",registerCount:"+registerCount+",loginCount:"+loginCount+",rechargeCount:"+rechargeCount+",investCount:"+investCount+",firstRecharge:"+firstRecharge+",cashCount:"+cashCount+",firstInvest:"+firstInvest);
		
		try{
			jt.update(UserSqlConstants.USERSTATISTIC_INSERTSQL,staticsDate,registerCount,loginCount,rechargeCount,investCount,firstRecharge,cashCount,firstInvest,lck_active,lck_un_active);
		}catch(Exception e){
			logger.info("批量处理插入失败，处理sql:"+UserSqlConstants.USERSTATISTIC_INSERTSQL+"处理数据:"+staticsDate+"错误信息:"+e);
		}

	}
}
